package View;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JScrollPane;

import Controller.ContactController;
import Controller.ContactDeleterAction;

/**
 * Dialog used to delete several contacts 
 * @author Vincent Francolin	
 * @version 1.0 : creation
 *
 */
public class ContactDeleterView extends JDialog {
	private JList list;
	private JScrollPane scrollList;
	private JButton ok;
	private JButton cancel;
	private DefaultListModel content;
	
	/**
	 * default constructor
	 * @param contacts the list of contacts 
	 * @param ctrl the main controller of the application
	 */
	public ContactDeleterView(ArrayList<String> contacts,ContactController ctrl){
		content = new DefaultListModel();
		for(String obj: contacts){
			 content.addElement(obj);
		}
		list = new JList(content);
		scrollList = new JScrollPane(list);
		ok = new JButton("Ok");
		cancel = new JButton("Cancel");
		scrollList.setPreferredSize(new Dimension(200,300));
		
		ok.addActionListener(new ContactDeleterAction(ctrl,this));
		cancel.addActionListener(new ContactDeleterAction(ctrl,this));
		initView();
		pack();
		this.setAlwaysOnTop(true);
	}
	
	private void initView(){
		this.setLayout(new GridBagLayout());
        GridBagConstraints cons = new GridBagConstraints();
        
        cons.insets = new Insets(10,10,10,10); 
        cons.gridy = 0;
        cons.gridx = 0;
        cons.gridwidth = 4;
        add(scrollList,cons);
        
        cons.gridy = 1;
        cons.gridx = 3;
        cons.gridwidth = 1;
        add(ok,cons);
        cons.gridx = 4;
        add(cancel,cons);
	}
	
	public int[] getSelected(){
		return list.getSelectedIndices();
	}
}
